package com.irdstudio.tdp.executor.core.batch.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 * 数据表操作类-[表名: 01.批次信息配置(BAT_BATCH_INFO_CONFIG)]
 * 通过JDBC实现基本的数据表操作(CRUD) 
 * @author 代码自动生成
 * @version 1.0
 * @date 2014-04-21
 */
public class BatBatchInfoConfigDao {

	/* 连接对象 */
	Connection conn = null;	

	public BatBatchInfoConfigDao(Connection conn){
		this.conn = conn;
	}

	/**
	 * 根据主键查询单条记录
	 * @param 
	 * @return
	 * @throws SQLException
	 */
	public BatBatchInfoConfig queryWithKeys(String batchId)
			throws SQLException {
		BatBatchInfoConfig dc = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn
					.prepareStatement("SELECT * FROM bat_batch_info_config WHERE batch_id=?");
			ps.setString(1,batchId);
			rs = ps.executeQuery();
			if (rs.next()) {
				dc = new BatBatchInfoConfig();
				dc.setBatchId(rs.getString("batch_id"));
				dc.setBatchName(rs.getString("batch_name"));
				dc.setSubsCode(rs.getString("subs_code"));
				dc.setLaunchType(rs.getString("launch_type"));
				dc.setBatchCronValue(rs.getString("batch_cron_value"));
				dc.setAgentId(rs.getString("agent_id"));
				dc.setValidDate(rs.getString("valid_date"));
				dc.setEquallyTaskAmount(rs.getInt("equally_task_amount"));
				dc.setIsRunAgain(rs.getString("is_run_again"));
				dc.setRemark(rs.getString("remark"));
			}
		} catch (SQLException e) {
			throw new SQLException("queryBatBatchInfoConfigWithKeys is Wrong!"
					+ e.getMessage());
		} finally {
			close(rs, null, ps);
		}
		return dc;
	}

	/**
	 * 根据查询条件查询多条或单条记录
	 * @param szCondition 
	 * @return
	 * @throws SQLException
	 */
	public List<BatBatchInfoConfig> queryWithCond(String szCondition,String szOrderBy)
			throws SQLException {
		List<BatBatchInfoConfig> dcList = new ArrayList<BatBatchInfoConfig>();
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement("SELECT * FROM BAT_BATCH_INFO_CONFIG "
					+ szCondition + " " + szOrderBy);
			rs = ps.executeQuery();
			while (rs.next()) {
				BatBatchInfoConfig dc = new BatBatchInfoConfig();
				dc.setBatchId(rs.getString("batch_id"));
				dc.setBatchName(rs.getString("batch_name"));
				dc.setSubsCode(rs.getString("subs_code"));
				dc.setLaunchType(rs.getString("launch_type"));
				dc.setBatchCronValue(rs.getString("batch_cron_value"));
				dc.setAgentId(rs.getString("agent_id"));
				dc.setValidDate(rs.getString("valid_date"));
				dc.setEquallyTaskAmount(rs.getInt("equally_task_amount"));
				dc.setIsRunAgain(rs.getString("is_run_again"));
				dc.setRemark(rs.getString("remark"));
				dcList.add(dc);
			}
		} catch (SQLException e) {
			throw new SQLException("queryBatBatchInfoConfigWithCond is Wrong!"
					+ e.getMessage());
		} finally {
			close(rs, null, ps);
		}
		return dcList;
	}

	/**
	 * 关闭资源
	 * @param theRs
	 * @param theStmt
	 * @param thePs
	 */
	protected void close(ResultSet theRs, Statement theStmt,
			PreparedStatement thePs) {
		try {
			if (theRs != null)
				theRs.close();
			if (theStmt != null)
				theStmt.close();
			if (thePs != null)
				thePs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}
